home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / comm / bbs / cit_src_AD08.lha / compute_net_stats.c < prev    next >
C/C++ Source or Header  |  1997-11-16  |  4KB  |  147 lines

  1. /**
  2.   functions for computing the network data traffic
  3. **/
  4. #include "ctdl.h"
  5. #include "string.h"
  6. #include "stdarg.h"
  7. #include "time.h"
  8. #define EXTERN extern
  9. #include "global_data.h"
  10.  
  11. extern char  *monthTab[13];
  12.  
  13. static char lognet[80];
  14. static char date2[6], date1[6];
  15. static void Write_Total_Data(void);
  16. long total_time;               /* total time for all net sessions */
  17. long total_char_in;            /* totals for all time */
  18. long total_char_out;           /* totals for all time */
  19. extern long char_in;                  /* totals for current time */
  20. extern long char_out;                 /* totals for current time */
  21. extern CONFIG      cfg;               /* Configuration variables      */
  22. extern long start_time;               /* total time for net session */
  23.  
  24. void makeAuditName(char *logfn, char *name);
  25. void Read_Total_Data(void);
  26. void getCdate(int *year, char **month, int *day, int *hours, int *minutes);
  27.  
  28. void makeAuditName(char *logfn, char *name)
  29.   {
  30.   char *ADir;
  31.   int i;
  32.   ADir = cfg.auditArea.saDirname;
  33.   i = strlen(ADir);
  34.   if( ADir[i-1] == '/' || ADir[i-1] == ':' )
  35.     {
  36.     sPrintf(logfn, "%s%s", ADir,  name);
  37.     }
  38.   else    sPrintf(logfn, "%s/%s", ADir,  name);
  39.   }
  40.  
  41. void Read_Total_Data()
  42.   {
  43.   char *mon;
  44.   FILE *ip;
  45.   int lyr, ldum;
  46.   if( cfg.Audit == 0 )return;
  47.   makeAuditName(lognet, "Network_stats.sys");
  48.   if( (ip = fopen(lognet, "r")) != NULL )
  49.     {
  50.     fread(&date1,         6, 1, ip);
  51.     fread(&total_time,    4, 1, ip);
  52.     fread(&total_char_in, 4, 1, ip);
  53.     fread(&total_char_out,4, 1, ip);
  54.     fclose(ip);
  55.     getCdate(&lyr,&mon,&ldum,&ldum,&ldum);
  56.     sprintf(date2,"%3s%2d",mon,lyr);      /* monthYear */
  57.     if( strcmp(date1,date2) != 0 )
  58.       {
  59.       /* reset, new month  */
  60.       getCdate(&lyr,&mon,&ldum,&ldum,&ldum);
  61.       sprintf(date1,"%3s%2d",mon,lyr);      /* monthYear */
  62.       total_time    = 0;
  63.       total_char_in = 0;
  64.       total_char_out= 0;
  65.       Write_Total_Data();
  66.       }
  67.     }
  68.   else   /* no file, create it */
  69.     {
  70.     getCdate(&lyr,&mon,&ldum,&ldum,&ldum);
  71.     sprintf(date1,"%3s%2d",mon,lyr);      /* monthYear */
  72.     total_time    = 0;
  73.     total_char_in  = 0;   /* new file */
  74.     total_char_out = 0;
  75.     Write_Total_Data();
  76.     }
  77.   }
  78.  
  79. static void Write_Total_Data()
  80.   {
  81.   FILE *ip;
  82.   if( cfg.Audit == 0 )return;
  83.   makeAuditName(lognet, "Network_stats.sys");
  84.   if( (ip = fopen(lognet, "w")) != NULL )
  85.     {
  86.     fwrite(date1,          6, 1, ip);
  87.     fwrite(&total_time,    4, 1, ip);
  88.     fwrite(&total_char_in, 4, 1, ip);
  89.     fwrite(&total_char_out,4, 1, ip);
  90.     fclose(ip);
  91.     }
  92.   }
  93.  
  94. void Compute_Data(char *name)
  95.   {
  96.   int lyr,ldum;
  97.   char *mon;
  98.   FILE *ip;
  99.   long cps;           /* computed data rate */
  100.   long work_time;
  101.   work_time = Set_Timer(start_time);
  102.   if( work_time == 0 ) work_time = 1;
  103.   cps = ( char_in + char_out ) / work_time;
  104.   printf(" Characters Input:%ld Characters Output:%ld  %ld cps\n", char_in, char_out, cps);
  105.   if( cfg.Audit  )
  106.     {
  107.     makeAuditName(lognet,"Network_Istats.sys");
  108.     if( (ip = fopen(lognet, "a")) != NULL)
  109.       {
  110.       getCdate(&lyr,&mon,&ldum,&ldum,&ldum);
  111.       sprintf(date2,"%3s%2d",mon,lyr);      /* monthYear */
  112.       fprintf(ip,"%s %20s %10ld %10ld %10ld\n",date2,name, char_in, char_out, work_time);
  113.       fclose(ip);
  114.       };
  115.     };
  116.   Read_Total_Data();
  117.   total_time     += work_time;
  118.   total_char_in  += char_in;
  119.   total_char_out += char_out;
  120.   Write_Total_Data();
  121.   }
  122. /*
  123.  * getCdate()
  124.  *
  125.  * This retrieves system date and returns in the parameters.
  126.  */
  127. void
  128. getCdate (int *year, char **month, int *day, int *hours, int *minutes)
  129. {
  130.   int       mon,
  131.             seconds,
  132.             milli;
  133.  
  134.   getRawDate (year, &mon, day, hours, minutes, &seconds, &milli);
  135.   *year -= 1900;
  136.   *month = monthTab[mon];
  137.  
  138. }
  139.  
  140. unsigned long Set_Timer(unsigned long oldtime ) /* return the delta from oldtime in microsecs*/
  141.   {
  142.   long t;
  143.   (void)time(&t);
  144.   return ( t - oldtime );
  145.   }
  146.  
  147.